home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / X / xshowcmap2 / xshowcmap2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  8.6 KB  |  359 lines

  1. /*
  2.  * xshowcmap - display the default colormap
  3.  *
  4.  * Copyright 1988 Siemens
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software and its
  7.  * documentation for any purpose and without fee is hereby granted, provided
  8.  * that the above copyright notice appear in all copies and that both that
  9.  * copyright notice and this permission notice appear in supporting
  10.  * documentation, and that the name of Siemens not be used in advertising or
  11.  * publicity pertaining to distribution of the software without specific,
  12.  * written prior permission.  Siemens makes no representations about the
  13.  * suitability of this software for any purpose.  It is provided "as is"
  14.  * without express or implied warranty.
  15.  *
  16.  * Author:  Claus Gittinger, Siemens Munich, unido!sinix!claus@uunet.uu.net
  17.  */
  18.  
  19. #include <X11/Xos.h>
  20. #include <X11/Xatom.h>
  21. #include <X11/Xlib.h>
  22. #include <X11/Xutil.h>
  23. #include <stdio.h>
  24.  
  25. Display     *dpy;
  26. int         screen;
  27. int         ncells, nhoriz, nvert;
  28. GC          gc;
  29.  
  30. #define NMAXCOLOR    256
  31. #define SQWIDTH        32
  32. #define SQHEIGHT    32
  33. #define BDWIDTH        1    /* separating grid */
  34.  
  35. int sqWidth = SQWIDTH;
  36. int sqHeight = SQHEIGHT;
  37. int mainWidth = 0;
  38. int mainHeight = 0;
  39.  
  40. Window      rootWindow, mainWindow;
  41.  
  42. #include "icon.bit"
  43.  
  44. main(argc,argv)
  45. int argc ;
  46. char *argv[] ;
  47. {
  48.     int i;
  49. #ifdef sgi
  50.     Visual *visualp;
  51. #else
  52.     Visual visual;
  53. #endif
  54.     XGCValues xgcv;
  55.     XSetWindowAttributes xswa;
  56.     XSizeHints sizehints;
  57.     char *ProgName, *server, *geom, *borderColor;
  58.     int borderWidth = 1;
  59.     extern double sqrt();
  60.     int h, v, color;
  61.         long BdPixel;
  62. #ifdef sgi
  63.     Colormap colormap;
  64.     int visualID=0;
  65.     int cmapID=0;
  66.     int visualDepth;
  67.     int cmapWindow = 0;
  68.     char buf1[100], buf2[20];
  69. #endif
  70.  
  71.     server = "";
  72.     geom   = "";
  73.     borderColor = "black";
  74.  
  75.     ProgName = argv[0];
  76.  
  77.     /********************************/
  78.     /** parse command line options **/
  79.     /********************************/
  80.  
  81.     for (i=1; i<argc; i++) {
  82.         if (argv[i][0] == '=')
  83.         geom = argv[i];
  84.         else if (index(argv[i],':') != NULL)
  85.         server = argv[i];
  86.         else if (strcmp(argv[i], "-bd") == 0)
  87.         borderColor = argv[++i];
  88.         else if (strcmp(argv[i], "-bw") == 0)
  89.         sscanf(argv[++i], "%d", &borderWidth);
  90. #ifdef sgi
  91.         else if (strcmp(argv[i], "-visid") == 0)
  92.         sscanf(argv[++i], "%i", &visualID);
  93.         else if (strcmp(argv[i], "-cmap") == 0)
  94.         sscanf(argv[++i], "%i", &cmapID);
  95.         else if (strcmp(argv[i], "-windowid") == 0)
  96.         sscanf(argv[++i], "%i", &cmapWindow);
  97. #endif
  98.         else
  99.         usage(ProgName);
  100.     }
  101. #ifdef sgi
  102.     if (cmapWindow && (cmapID || visualID))
  103.         usage(ProgName);
  104. #endif
  105.  
  106.     dpy = XOpenDisplay(server);
  107.     if (dpy == NULL) {
  108.         fprintf(stderr, "can't open display \"%s\"\n",server);
  109.         exit(1);
  110.     }
  111.     screen = DefaultScreen(dpy);
  112. #ifdef sgi
  113.     /* if they specified a window, get its visual and colormap */
  114.     if (cmapWindow)
  115.     {
  116.         XWindowAttributes attr;
  117.         XGetWindowAttributes(dpy, (Window)cmapWindow, &attr);
  118.         visualp = attr.visual;
  119.         visualDepth = attr.depth;
  120.         colormap = attr.colormap;
  121.     }
  122.     /* If an explicit visualID is set use it and the colormap*/
  123.     else if (visualID)
  124.     {
  125.         XVisualInfo* visinfo;
  126.         XVisualInfo template;
  127.         int nret;
  128.  
  129.         template.visualid = visualID;
  130.         template.screen = screen;
  131.         visinfo = XGetVisualInfo(dpy,
  132.                      VisualScreenMask|VisualIDMask,
  133.                      &template, &nret);
  134.         if (!visinfo)
  135.         {
  136.         fprintf(stderr, "%s:  Can't get visual info\n", argv[0]);
  137.         exit(1);
  138.         }
  139.         visualp = visinfo->visual;
  140.         visualDepth = visinfo->depth;
  141.         colormap = cmapID;
  142.     }
  143.     /* no visualID set, use the default */
  144.     else
  145.     {
  146.         visualp = DefaultVisual(dpy, screen);
  147.         visualDepth = DefaultDepth(dpy, screen);
  148.         colormap = DefaultColormap(dpy, screen);
  149.     }
  150.         ncells = 1<<visualDepth;
  151. #else
  152.         ncells = DisplayCells(dpy, screen);
  153. #endif
  154.  
  155. #ifndef sgi
  156.     /*
  157.      * actually this is not needed;
  158.      * but had problems sometimes with bad rgb-data bases
  159.      * to make xshowcmap run also in this case ...
  160.      */
  161.     if (strcmp(borderColor, "black") == 0)
  162.         BdPixel = BlackPixel(dpy, screen);
  163.     else
  164. #endif
  165.         BdPixel = requestColor(borderColor);
  166.  
  167.     /*************************************/
  168.     /** configure the window size hints **/
  169.     /*************************************/
  170.  
  171.     nhoriz = (int)sqrt((double)ncells);
  172.     nvert = nhoriz;
  173.     while (nhoriz * nvert < ncells)
  174.         nvert++;
  175.  
  176.     sizehints.flags = PMinSize | PPosition | PSize;
  177.     sizehints.min_width = sqWidth;
  178.     sizehints.min_height = sqHeight;
  179.     sizehints.width = nhoriz*sqWidth + (nhoriz-1)*BDWIDTH;
  180.     sizehints.height = nvert*sqHeight + (nvert-1)*BDWIDTH;
  181.     sizehints.x = 100;
  182.     sizehints.y = 300;
  183.  
  184.     if (geom && strlen(geom)) {
  185.         int flags;
  186.         unsigned int width, height ;
  187.  
  188.         flags = XParseGeometry(geom, &sizehints.x, &sizehints.y,
  189.                      &width, &height);
  190.         if (flags & (XValue | YValue))
  191.         sizehints.flags |= USPosition;
  192.     }
  193.  
  194.     /*******************************************************************/
  195.     /** create the palette main window and set its standard properties **/
  196.     /*******************************************************************/
  197.  
  198.     xswa.event_mask = KeyPressMask | StructureNotifyMask;
  199.     xswa.event_mask |= ExposureMask;
  200.  
  201.     xswa.background_pixel = BdPixel;
  202.     xswa.border_pixel = BdPixel;
  203. #ifdef sgi
  204.     xswa.colormap = colormap;
  205. #else
  206.  
  207.     visual.visualid = CopyFromParent;
  208. #endif
  209.  
  210.     rootWindow = RootWindow(dpy, screen);
  211.  
  212.     mainWindow = XCreateWindow(dpy, rootWindow,
  213.                    sizehints.x, sizehints.y,
  214.                    sizehints.width, sizehints.height,
  215.                    borderWidth,
  216. #ifdef sgi
  217.                    visualDepth,
  218. #else
  219.                    DefaultDepth(dpy,screen),
  220. #endif
  221.                    InputOutput,
  222. #ifdef sgi
  223.                    visualp,
  224.                    CWEventMask | CWBackPixel | CWBorderPixel | CWColormap,
  225. #else
  226.                    &visual,
  227.                    CWEventMask | CWBackPixel | CWBorderPixel,
  228. #endif
  229.                    &xswa);
  230.  
  231.     /*******************************************************************/
  232.     /** define its name and icon-bitmap                               **/
  233.     /*******************************************************************/
  234.  
  235. #ifdef sgi
  236.     /*XSetStandardProperties(dpy, mainWindow, "colors","Colors",*/
  237.     buf1[0] = '\0';
  238.     if (cmapWindow)
  239.     {
  240.         sprintf (buf2,"window %x ", cmapWindow);
  241.         strcat(buf1,buf2);
  242.     }
  243.     if (visualID)
  244.     {
  245.         sprintf (buf2,"visual %x ",visualID);
  246.         strcat(buf1,buf2);
  247.     }
  248.     if (cmapID)
  249.     {
  250.         sprintf (buf2,"cmap %x ",cmapID);
  251.         strcat(buf1,buf2);
  252.     }
  253.     strcat(buf1,"colors");
  254.     XSetStandardProperties(dpy, mainWindow, buf1, "Colors",
  255. #else
  256.     XSetStandardProperties(dpy, mainWindow, "colors","Colors",
  257. #endif
  258.                    XCreateBitmapFromData(dpy, rootWindow,
  259.                              icon_bits,
  260.                              icon_width, icon_height),
  261.                    argv, argc, &sizehints);
  262.  
  263.         gc = XCreateGC(dpy, mainWindow, 0L, NULL);
  264.  
  265.     XMapWindow(dpy, mainWindow);
  266.  
  267.     eventloop();
  268.     exit(0) ;
  269. }
  270.  
  271. usage(name)
  272. char *name;
  273. {
  274.         fprintf(stderr, "usage: %s [geometry] [display]\n", name);
  275.         fprintf(stderr, "          [-bd color] [-bw number]\n");
  276. #ifdef sgi
  277.         fprintf(stderr, "          [[-visid visualID] [-cmap colormap]]|[-windowid window]\n");
  278. #endif
  279.         exit(1);
  280. }
  281.  
  282. requestColor(name)
  283. char *name;
  284. {
  285.     XColor truecolor, availcolor;
  286.  
  287.     if (XAllocNamedColor(dpy,
  288.                  DefaultColormap(dpy, DefaultScreen(dpy)),
  289.                  name, &availcolor, &truecolor) == 0) {
  290.         fprintf(stderr, "Color '%s' unknown\n", name);
  291.         exit(1);
  292.     }
  293.     if (truecolor.red != availcolor.red ||
  294.         truecolor.green != availcolor.green ||
  295.         truecolor.blue != availcolor.blue) {
  296.         fprintf(stderr, "Warning: %s color bay be wrong\n", name);
  297.     }
  298.  
  299.     return availcolor.pixel;
  300. }
  301.  
  302. eventloop() {
  303.     XEvent event;
  304.     XExposeEvent *ee = (XExposeEvent *)&event;
  305.     XKeyPressedEvent *ke = (XKeyPressedEvent *)&event;
  306.     XButtonPressedEvent *be = (XButtonPressedEvent *)&event;
  307.         XConfigureEvent *ce = (XConfigureEvent *)&event;
  308.     char keybuff[10];
  309.     int nchar;
  310.     int color, h, v;
  311.  
  312.     while(1) {
  313.         XNextEvent(dpy, &event);
  314.         switch(event.type) {
  315.         case KeyPress:
  316.             nchar = XLookupString(ke, keybuff, 1, NULL, NULL);
  317.  
  318.             if (nchar > 0) {
  319.             switch (keybuff[0]) {
  320.                 case 'q':
  321.                 case 'Q':
  322.                 exit(0);
  323.             }
  324.             }
  325.             break;
  326.  
  327.                 case ConfigureNotify:
  328.             if (ce->width == mainWidth && ce->height == mainHeight)
  329.             break;
  330.             sqWidth = ce->width / nhoriz;
  331.             sqHeight = ce->height / nvert;
  332.             color = 0;
  333.             for (v=0; v<nvert; v++)
  334.                 for (h=0; h<nhoriz; h++, color++) {
  335.                 XSetForeground(dpy, gc, color);
  336.                 XFillRectangle(dpy, mainWindow, gc, 
  337.                       h*(sqWidth+1), v*(sqHeight+1), sqWidth, sqHeight);
  338.             }
  339.                     break;
  340.  
  341.         case Expose:
  342.             if (ee->count)
  343.             break;
  344.  
  345.             color = 0;
  346.             for (v=0; v<nvert; v++)
  347.                 for (h=0; h<nhoriz; h++, color++) {
  348.                 XSetForeground(dpy, gc, color);
  349.                 XFillRectangle(dpy, mainWindow, gc, 
  350.                       h*(sqWidth+1), v*(sqHeight+1), sqWidth, sqHeight);
  351.                 }
  352.             break;
  353.  
  354.         default:
  355.             break;
  356.         }
  357.     }
  358. }
  359.